﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading;
namespace A.HTMlServer
{
    public class DCommon
    {
        private string Eorro=null;
        /// <summary>
        /// 构造方法
        /// </summary>
        public DCommon()
        {
 
        }

        /// <summary>
        /// 获取网页的HTML内容
        /// </summary>
        /// <param name="url">url</param>
        /// <returns></returns>
        public string GetStrHtml(string url)
        {
            return GetStrHtml(url, null);
        }


        /// <summary>
        ///  获取网页的HTML内容
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="encoding">Encoding</param>
        /// <returns></returns>
        public string GetStrHtml(string url, Encoding encoding)
        {
            try
            {
                byte[] buf = new WebClient().DownloadData(url);
                if (encoding != null) return encoding.GetString(buf);
                string html = Encoding.UTF8.GetString(buf);
                encoding = GetEncoding(html);
                if (encoding == Encoding.UTF8) return html;
                return encoding.GetString(buf);
            }
            catch
            {
                Eorro = "没有连接";
                //throw ex;
                return null;
            }
        }

        /// <summary>
        /// 根据网页的HTML内容提取网页的Encoding
        /// </summary>
        /// <param name="html">HTML</param>
        /// <returns></returns>
        private Encoding GetEncoding(string html)
        {

            string pattern = @"(?i)\bcharset=(?<charset>[-a-zA-Z_0-9]+)";

            string charset = Regex.Match(html, pattern).Groups["charset"].Value;

            try { return Encoding.GetEncoding(charset); }

            catch (ArgumentException) { return null; }

        }
        /// <summary>
        /// 获回一个时间的图片名
        /// </summary>
        /// <returns></returns>
        private string GetFileName()
        {
            return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
        }

        /**/
        /// <summary>
        ///Web Client Method ,only For Small picture
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="url"></param>
        /// <param name="localPath"></param>
        void DownloadOneFileByURLWithWebClient(string url, string fileName)
        {

            fileName = GetFileExists(fileName);
            if (fileName != null)
            {
                //System.Net.WebClient wc = new System.Net.WebClient();
                //wc.DownloadFile(url, localPath + fileName);
            }


        }

        /// <summary>
        /// 文件的判断是否存在，存在时加"_1"
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns>新的文件名</returns>
        private string GetFileExists(string fileName)
        {
        //    if (File.Exists(localPath + fileName))
        //    {
        //        //File.Delete(localPath +fileName);//存在时删除
        //        if (!isover)
        //        {
        //            return null;
        //        }
        //        ifile++;
        //        if (ifile == 1)
        //        {
        //            fileName = System.IO.Path.GetFileNameWithoutExtension(fileName) + "(" + ifile.ToString() + ")" + Path.GetExtension(fileName);
        //        }
        //        else
        //        {
        //            fileName = fileName.Replace("(" + (ifile - 1).ToString() + ")", "(" + ifile.ToString() + ")");
        //        }
        //        fileName = GetFileExists(fileName);//在次判断是存在,递归调用

        //    }
        //    ifile = 0;
            return fileName;
        }

        /// <summary>
        /// 文件夹检查，不存在就新建
        /// </summary>
        private void GetDirectory(string localPath)
        {
            if (Directory.Exists(localPath) == false) { Directory.CreateDirectory(localPath); }
        }

        /// <summary>
        /// 获取所有采集URL
        /// </summary>
        /// <param name="Html">Html</param>
        /// <param name="strRegex">Regex</param>
        /// <param name="i">I</param>
        /// <returns>string</returns>
        public string GetPartHtml(string Html, string strRegex,int i)
        {
            Regex r = new Regex(strRegex, RegexOptions.Singleline);
            Match m = r.Match(Html);
            if (m.Success)
            {
                if (m.Groups.Count > i)
                {
                    return m.Groups[i].Value;
                }
                return m.Value;
            }
            return "";
        }

        /// <summary>
        /// 获取中文
        /// </summary>
        /// <param name="Html"></param>
        /// <returns></returns>
        public string GetChinese(string Html)
        {
            Regex r = new Regex(@"^[\u4e00-\u9fa5]+.*?[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase);
            Match m = r.Match(Html);
            if (m.Success)
            {
                return m.Value;
            }
            return "";
        }

        /// <summary>
        /// 替换所有的超链接
        /// </summary>
        /// <param name="Html"></param>
        /// <returns></returns>
        public string RemoveA(string Html)
        {
            //<A class=blue href="http://face.39.net/mrhf/hfcs/" target=_blank>护肤</A>
            return Regex.Replace(Html, @"<a.*?>(.*?)</a>", "$1",RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 替换所指定str
        /// </summary>
        /// <param name="Html"></param>
        /// <returns></returns>
        public string RemovePart(string Html,string pattern,string strvalue)
        {
            return Regex.Replace(Html, pattern, strvalue,RegexOptions.Singleline|RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 获取多少匹配的值
        /// </summary>
        /// <param name="Html"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public MatchCollection GetMatchs(string Html, string pattern)
        {
            Regex R = new Regex(pattern,RegexOptions.IgnoreCase);
            return R.Matches(Html);
        }

        /// <summary>
        /// 返回是不否匹配
        /// </summary>
        /// <param name="Html"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public bool GetIsMatch(string Html, string pattern)
        {
            return Regex.IsMatch(Html,pattern);
        }
    }
}